import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.util.Iterator;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JLabel;

import org.openstreetmap.osm.Settings;
import org.openstreetmap.osm.data.MemoryDataSet;
import org.openstreetmap.osm.data.coordinates.LatLon;
import org.openstreetmap.osmosis.core.domain.v0_6.Node;
import org.openstreetmap.travelingsalesman.gui.MapMover;
import org.openstreetmap.travelingsalesman.gui.widgets.MapPanel;
import org.openstreetmap.travelingsalesman.navigation.NavigationManager;
import org.openstreetmap.travelingsalesman.routing.IRouter;
import org.openstreetmap.travelingsalesman.routing.Route;
import org.openstreetmap.travelingsalesman.routing.Route.RoutingStep;
import org.openstreetmap.travelingsalesman.routing.routers.TurnRestrictedAStar;
import org.openstreetmap.travelingsalesman.routing.selectors.Motorcar;

public class Routing {

	public static void main(String[] args) {
		File file = new File("Primaharta.osm");

		MemoryDataSet map = (new org.openstreetmap.osm.io.FileLoader(file))
				.parseOsm();
		Motorcar vehicle = new Motorcar();
		LatLon startCoord = new LatLon(46.7588865, 23.5985892);
		Node startNode = map.getNearestNode(startCoord, vehicle);
		System.out.println(startNode.getId());
		LatLon targetCoord = new LatLon(46.7797955, 23.6228551);
		
		Node targetNode = map.getNearestNode(targetCoord, null);
		System.out.println(targetNode.getId());

		IRouter router = new TurnRestrictedAStar();
		
		Route theRoute = router.route(map, startNode, targetNode, vehicle);
		List<RoutingStep> steps = theRoute.getRoutingSteps();
		Iterator<RoutingStep> it = steps.iterator();
		while (it.hasNext()) {
			RoutingStep current = it.next();
			System.out.println(current.toString() + " " + current.getWay());
		}

		NavigationManager nav = new NavigationManager();
		nav.setMap(map);

//		SmoothTilePainter paint = new SmoothTilePainter();

		JFrame frame = new JFrame("Routare");
		frame.setVisible(true);
		frame.addWindowListener(new WindowAdapter() {

			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		// This is an empty content area in the frame
		JLabel jlbempty = new JLabel("");
		jlbempty.setPreferredSize(new Dimension(175, 100));
		frame.getContentPane().add(jlbempty, BorderLayout.CENTER);
		
		MapPanel mapPanel = new MapPanel();
		mapPanel.setLayout(new BorderLayout());
		mapPanel.setDataSet(map);
		final Settings settings = Settings.getInstance();
		final double latitude = settings.getDouble("state.MapPanel.latitude",
				46);
		final double longitude = settings.getDouble("state.MapPanel.longitude",
				23);
		LatLon center = new LatLon(latitude, longitude);
		// if (center.isOutSideWorld()) {
		// center = SimpleMapPanel.DEFAULTCENTER;
		// }
		// carefull this is multiplied scale value
		mapPanel.setPreferredSize(new Dimension(1600, 1200));
		System.out.println(mapPanel.getScale());
		double scale = settings.getDouble("state.MapPanel.scale",
				10 * mapPanel.getScale()
						* Settings.getProjection().scaleFactor());
		if (scale > MapMover.MAXSCALE) {
			scale = MapMover.MAXSCALE;
		} else if (scale < MapMover.MINSCALE) {
			scale = MapMover.MINSCALE;
		}
		System.out.println(scale);

		mapPanel.zoomTo(mapPanel.getProjection().latlon2eastNorth(center),
				scale / mapPanel.getProjection().scaleFactor());
		mapPanel.setCurrentRoute(theRoute);

		frame.getContentPane().add(mapPanel, BorderLayout.CENTER);

		frame.pack();
		frame.setVisible(true);
		// paint.visitRoute(map,steps);
		// assertTrue(theRoute.getSegmentList().size() > 0);

	}
}
